/*
 * File:   ByteArrayIdentifier.java
 * Author: Oleg S. Estekhin
 */
package dht.emulator;

import java.util.Arrays;
import java.util.Random;

/** Provides a byte-array identifier. */
public final class ByteArrayIdentifier extends Identifier
{

    private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


    /**
     * Creates a new random identifier.
     *
     * @param length the identifier length in bytes
     *
     * @return a new random identifier.
     */
    public static Identifier randomIdentifier(int length)
    {
        Random random = new Random();
        byte[] data = new byte[length];
        random.nextBytes(data);
        return new ByteArrayIdentifier(data);
    }

    /**
     * Creates a new random identifier.
     *
     * @param length the identifier length in bytes
     * @param random the random generator
     *
     * @return a new random identifier.
     */
    public static Identifier randomIdentifier(int length, Random random)
    {
        byte[] data = new byte[length];
        random.nextBytes(data);
        return new ByteArrayIdentifier(data);
    }


    private final byte[] data;

    public byte[] getByteVorstellung()
    {
        return data;
    }


    /**
     * Creates a new <code>ByteArrayIdentifier</code> object.
     *
     * @param data the identifier data.
     *
     * @throws IllegalArgumentException if <code>data</code> is <code>null</code>
     */
    public ByteArrayIdentifier(byte[] data)
    {
        if (data == null)
        {
            throw new IllegalArgumentException("data is null");
        }
        this.data = data.clone();
    }


    /**
     * Returns the identifier length.
     *
     * @return the identifier length
     */
    @Override
    public int getLength()
    {
        return data.length;
    }

    /**
     * Evaluates a distance to the identifier
     *
     * @param identifier the identifier to evaluate distance to
     *
     * @return a distance to the identifier
     *
     * @throws NullPointerException if <code>identifier</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>identifier</code> has different length
     */
    @Override
    public Identifier getDistance(Identifier identifier)
    {
        if (identifier == null)
        {
            throw new NullPointerException("identifier is null");
        }
        if (!(identifier instanceof ByteArrayIdentifier))
        {
            throw new IllegalArgumentException("invalid identifier type");
        }
        ByteArrayIdentifier id = (ByteArrayIdentifier) identifier;
        if (data.length != id.data.length)
        {
            throw new IllegalArgumentException("invalid identifier length");
        }
        byte[] distance = new byte[data.length];
        for (int i = 0; i < distance.length; i++)
        {
            distance[i] = (byte) (data[i] ^ id.data[i]);
        }
        return new ByteArrayIdentifier(distance);
    }


    @Override
    public int compareTo(Identifier identifier)
    {
        if (identifier == null)
        {
            throw new NullPointerException("identifier is null");
        }
        if (!(identifier instanceof ByteArrayIdentifier))
        {
            throw new IllegalArgumentException("invalid identifier type");
        }
        ByteArrayIdentifier id = (ByteArrayIdentifier) identifier;
        if (data.length != id.data.length)
        {
            throw new IllegalArgumentException("invalid identifier length");
        }
        for (int i = 0; i < data.length; i++)
        {
            int a = data[i] & 0xff;
            int b = id.data[i] & 0xff;
            if (a < b)
            {
                return -1;
            }
            else if (a > b)
            {
                return 1;
            }
        }
        return 0;
    }


    @Override
    public int hashCode()
    {
        return Arrays.hashCode(data);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        else if (obj instanceof ByteArrayIdentifier)
        {
            ByteArrayIdentifier id = (ByteArrayIdentifier) obj;
            return Arrays.equals(data, id.data);
        }
        else
        {
            return false;
        }
    }

    @Override
    public String toString()
    {
        StringBuilder buffer = new StringBuilder(data.length * 2);
        for (byte b : data)
        {
            buffer.append(HEX[(b & 0xF0) >>> 4]);
            buffer.append(HEX[b & 0x0F]);
        }
        return buffer.toString();
    }

}